package com.newvpn.fastbest.zenith.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.newvpn.fastbest.zenith.R;
import com.newvpn.fastbest.zenith.presenter.VPNDetailInfo;
import com.newvpn.fastbest.zenith.presenter.VPNPresenter;


public class ConnectResultActivity extends BaseActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.v_activity_result);
        initViews();
    }



    private void initViews() {
        findViewById(R.id.ivBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        VPNDetailInfo detailInfo = VPNPresenter.INSTANCE.getConnectedVpnInfoServer();
        if (detailInfo == null){
            finish();
            return;
        }
        TextView tvTime = findViewById(R.id.tvTime);
        ImageView ivCountry = findViewById(R.id.ivCountry);
        TextView tvIp = findViewById(R.id.tvIp);
        TextView tvCountry = findViewById(R.id.tvCountry);
        long time = System.currentTimeMillis() - detailInfo.startTime;
        String formattedTime = formatSecondsToHHMMSS((int)(time/1000));
        tvTime.setText(formattedTime);
        tvIp.setText(detailInfo.serverIp);
        tvCountry.setText(detailInfo.countryName);
        if (TextUtils.isEmpty(detailInfo.iconUrl)){
            ivCountry.setImageResource(R.drawable.v_nodeselect);
        }else {
            Glide.with(getApplicationContext()) // 使用当前上下文，对于 Fragment，使用 getActivity() 或者 requireActivity()
                    .load(detailInfo.iconUrl)
                    .placeholder(R.drawable.bg_circle) // 加载中的占位图
                    .error(R.drawable.bg_circle)
                    .into(ivCountry);
        }


    }

    public  String formatSecondsToHHMMSS(int seconds) {
        int hours = seconds / 3600; // 计算小时数
        int minutes = (seconds % 3600) / 60; // 计算分钟数
        int secondsRemaining = seconds % 60; // 计算剩余秒数

        // 使用String.format来确保每个部分都是两位数
        return String.format("%02d:%02d:%02d", hours, minutes, secondsRemaining);
    }



}
